﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Foundation;
using Marmat.Forms;
using System.Reflection;

namespace Marmat.Template.WinForm
{
    /// <summary>
    /// 关联网格组件。
    /// </summary>
    public class AdvRelationGrid : Marmat.Forms.GridControl, IProvider
    {
        /// <summary>
        /// 获取或设置默认的筛选条件
        /// </summary>
        [DefaultValue("")]
        [Browsable(false)]
        public string Filter { get; set; }

        /// <summary>
        /// 设置服务提供者
        /// </summary>
        /// <typeparam name="Entity"></typeparam>
        public AdvRelationGridImplement<Entity> SetProvider<Entity>(params string[] columns)
        {
            void action(Entity[] rowCollection)
            {
                foreach (var record in rowCollection)
                {
                    var item = this.AddNew();
                    if (item is DataRow dataRow)
                    {
                        foreach (var dcName in columns)
                        {
                            var member = record.GetType().GetMembers().FirstOrDefault(x => (x is FieldInfo || x is PropertyInfo) && x.Name == dcName);
                            if (member is FieldInfo field)
                                dataRow[dcName] = field.GetValue(item);
                            if (member is PropertyInfo property)
                                dataRow[dcName] = property.GetValue(item);
                        }
                    }
                    else
                    {
                        foreach (var dcName in columns)
                        {
                            var get_member = record.GetType().GetMembers().FirstOrDefault(x => (x is FieldInfo || x is PropertyInfo) && x.Name == dcName);
                            var set_member = item.GetType().GetMembers().FirstOrDefault(x => x.Name == dcName && x is FieldInfo || x is PropertyInfo);
                            object get_value = null;
                            {
                                if (get_member is FieldInfo field)
                                    get_value = field.GetValue(record);
                                if (get_member is PropertyInfo property)
                                    get_value = property.GetValue(record);
                            }
                            {
                                if (set_member is FieldInfo field)
                                    field.SetValue(item, get_value);
                                else if (set_member is PropertyInfo property)
                                    property.SetValue(item, get_value);
                            }
                        }
                    }
                }
            }
            return SetProvider((Action<Entity[]>)action);
        }

        /// <summary>
        /// 设置服务提供者
        /// </summary>
        /// <typeparam name="Entity"></typeparam>
        public AdvRelationGridImplement<Entity> SetProvider<Entity>(Action<Entity[]> action)
        {
            return new AdvRelationGridImplement<Entity>(this, action);
        }

    }
}
